﻿package purplemovies.showreel
{
	import purplemovies.events.VideoEvent;
	
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	
	import flash.net.NetStream;
	public class SRStream extends NetStream {
		// Vars:
		public var url					:String;	// URL for video file.
		public var title_url			:String;	// URL for SWF for title.
		public var title_start_frame	:int;		// Where to play form when title is first displayed.
		public var title_end_frame		:int;		// Where to play from when we're finished with the title.
		
		public function get loaded():Boolean {	return _loaded;		}
		
		private var _video_info			:Object;	// Meta data for video file.
		private var _loaded				:Boolean;	// Flag set after file is loaded in.
		private var _timer				:Timer;		// Timer for interval loop to check loaded status.
		private var _last_bytes			:Number;	// The bytes loaded last time timer check them.
		
		public function get video_info():Object{	return _video_info;		}
		// Video's dimentions in pixels:
		public function get dimentions():Point {	return new Point(_video_info.width, _video_info.height);		}
		
		// Constructor:
		function SRStream(connection:NetConnection, p_url:String, p_title_url:String=null){
			_loaded = false;
			_video_info			= null;
			
			super(connection);
			client = this;
			url 				= p_url;
			title_url			= p_title_url;
			
			_last_bytes = bytesLoaded;
			_timer = new Timer(33); // Hard coded at about 30 fps...
			_timer.addEventListener( TimerEvent.TIMER, onTimer );
			addEventListener(NetStatusEvent.NET_STATUS, onStatusChange);
		}
		
		public function loadFile( p_url ){
			play( p_url );
			_timer.start();
		}
		
		private function onTimer(e){
			
			if( bytesLoaded > _last_bytes ){
				//trace( 'loaded :: ', bytesLoaded );
				var ve = new VideoEvent(VideoEvent.DATA_RECEIVED);
				ve.info = {};
				ve.info.loaded = bytesLoaded; ve.info.total = bytesTotal;
				dispatchEvent( ve );
			}
			_last_bytes = bytesLoaded;
			if( bytesLoaded >= bytesTotal && bytesTotal > 0 ){
				_loaded = true;
				_timer.removeEventListener( TimerEvent.TIMER, onTimer );
				_timer = null;
				dispatchEvent( new VideoEvent(VideoEvent.VIDEO_LOADED) );
				//trace('loaded');
			}
		}
		private function onStatusChange(o){
			//trace(o.info.code);
			switch( o.info.code ){
				case "NetStream.Play.Stop": dispatchEvent( new VideoEvent(VideoEvent.END_OF_VIDEO) ); break;
			}
		}

		public function onCuePoint(cuePoint:Object):void {
			//trace('cue point :: ', cuePoint);
			var ve = new VideoEvent( VideoEvent.CUE_POINT );
			ve.info = cuePoint;
			dispatchEvent( ve );
		}
		public function onMetaData(o){
			_video_info = o;
			var ve = new VideoEvent( VideoEvent.META_DATA );
			ve.info = o;
			dispatchEvent( ve );
			
			for( var i in o.cuePoints ){
				o.cuePoints[i].index = i;
				/*
				trace( o.cuePoints[i].parameters.start );
				trace( o.cuePoints[i].parameters.end );*/
				/*
				trace(i + ": " + o.cuePoints[i]);
				trace( o.cuePoints[i].index );*/
			}

		}
		
	}
}